package com.lyluly.coursemgmt.webmodule.controller;

import com.lyluly.coursemgmt.servicemodule.iService.ICourseService;
import com.lyluly.coursemgmt.servicemodule.model.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @version v1.0
 * @author clc
 */
@Controller
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private ICourseService iCourseService;

    /*只声明void，会默认还是访问courses.jsp，解决方法：
        * 1、加上@ResponseBody
        * 2、传入HttpServletResponse参数*/

    /**
     * 添加一门课程
     * @param course 课程实体类
     * @param response
     */
    @PostMapping
    public void addCourse(@RequestBody Course course, HttpServletResponse response){
        iCourseService.addCourse(course);
    }

    /**
     * 根据课程名删除一门课程
     * @param courseName 课程名
     * @param response
     */
    @DeleteMapping("/{courseName}")
    public void removeCourse(@PathVariable String courseName, HttpServletResponse response){
        response.setHeader("Allow","DELETE");
        iCourseService.removeCourse(courseName);
    }

    /**
     * 根据课程名修改一门课程
     * @param course 课程实体类
     * @param response
     */
    @PutMapping
    public void modifyCourse(@RequestBody Course course, HttpServletResponse response){
        iCourseService.modifyCourse(course);
    }

    /**
     * 查找所有课程
     * @return 返回所有课程
     */
    @GetMapping
    @ResponseBody
    public List<Course> findByCourseList(){
        return iCourseService.findByCourseList();
    }

    /**
     * 根据课程名查找一门课程
     * @param courseName
     * @return 返回相应课程
     */
    @GetMapping("/{courseName}")
    @ResponseBody
    public Course findByCourseName(@PathVariable String courseName){
        return iCourseService.findByCourseName(courseName);
    }

}
